package com.zhouyibike.uu.ui.activity.mywallet;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.LoginResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.DataSingle;
import com.zhouyibike.uu.utils.TextUtils;

import java.util.HashMap;
import java.util.Map;

public class ChongzhimimaActivity2 extends BaseActivity {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private Button btn_logout;
    private Dialog jubaodialog;
    private Animation operatingAnim;
    private EditText pwd2,pwd1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chongzhimima2);
        initView();
        setjubaoDialog();
        initListener();
    }



    private void initListener() {
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (pwd1.getText().toString().trim().length()<6||pwd2.getText().toString().trim().length()<6)
                {
                    toastShow("密码需设定为6位数字");
                    return;
                }
                if (pwd1.getText().toString().equals(pwd2.getText().toString()))
                {
                    xiugaiyanzhengCode();
                }else{
                    toastShow("两次输入密码不一致");
                }
            }
        });
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("重置密码");
        btn_logout= (Button) findViewById(R.id.btn_logout);
        pwd1= (EditText) findViewById(R.id.newpwd1);
        pwd1.addTextChangedListener(new EditTextWatcher1());
        pwd2= (EditText) findViewById(R.id.newpwd2);
        pwd2.addTextChangedListener(new EditTextWatcher2());
    }
    //提现密码修改成功
    private void setjubaoDialog() {
        TextView jubaookknow,sucesstitle,notes;
        LayoutInflater inflater = LayoutInflater.from(ChongzhimimaActivity2.this);
        final View headview = inflater.inflate(R.layout.jubao_ok_dialog, null, false);
        jubaookknow = headview.findViewById(R.id.jubaookknow);
        sucesstitle=headview.findViewById(R.id.sucesstitle);
        notes=headview.findViewById(R.id.notes);
        sucesstitle.setText("修改成功");
        notes.setText("你的提现密码已经重置,"+"\n"+"请使用新的提现密码进行提现");
        jubaodialog = new Dialog(this, R.style.Dialog_Fullscreen);
        jubaodialog.setContentView(headview, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        jubaodialog.setCanceledOnTouchOutside(false);
        Window dialogWindow = jubaodialog.getWindow();
        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
//        adjustPan|stateHidden
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
//                lp.x = 100; // 新位置X坐标
//          lp.y = 100; // 新位置Y坐标
        lp.width = (int) (DataSingle.getInstance().PhoneW * 0.75); // 宽度
        lp.height = (int) (DataSingle.getInstance().PhoneW * 0.75);
//         lp.alpha = 0.7f; // 透明度
        // 当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes
        // dialog.onWindowAttributesChanged(lp);
        dialogWindow.setWindowAnimations(R.style.mypopwindow_anim_style3);  //添加动画
        dialogWindow.setAttributes(lp);
        jubaookknow.setText("返回首页");
        jubaookknow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jubaodialog.dismiss();
                setResult(1);
                finish();
            }
        });
        DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        };
        jubaodialog.setOnKeyListener(keylistener);
    }
    /**
     * 修改提现密码
     */
    private void xiugaiyanzhengCode() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("newPwd", pwd1.getText().toString());
        showProgressDialog();
        addSubscription(apiStores.ModifyPwdByName(params),
                new ApiCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                           // toastShow(model.getMessage());
                            jubaodialog.show();
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
    class EditTextWatcher1 implements TextWatcher {

        public void afterTextChanged(Editable edt) {

            //手机号不为空，且为11位
            if (!TextUtils.isNullorEmpty(pwd1.getText().toString()) && pwd1.getText().toString().length()==6) {

            } else if (pwd1.getText().toString().length()>6){
                pwd1.setText(pwd1.getText().toString().substring(0,6));
                pwd1.setSelection(6);
                toastShow("密码需设定为6位数字");
            }
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

    }
    class EditTextWatcher2 implements TextWatcher {

        public void afterTextChanged(Editable edt) {

            //手机号不为空，且为11位
            if (!TextUtils.isNullorEmpty(pwd2.getText().toString()) && pwd2.getText().toString().length()==6) {

            } else if (pwd2.getText().toString().length()>6){
                pwd2.setText(pwd2.getText().toString().substring(0,6));
                pwd2.setSelection(6);
                toastShow("密码需设定为6位数字");
            }
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

    }
}
